import OSS from 'ali-oss';
const client = new OSS({
  region: 'oss-cn-qingdao',
  accessKeyId: 'LTAI0oUoBK7GwQn4',
  accessKeySecret: 'gHqAlhq2pX57Jz8XCO0zMLsKlZoAlX',
  bucket: 'pakj'
});
export default {
  upload(base64, dir = 'infouploader') {
    return new Promise((resolve, reject) => {
      var date = new Date();
      var time =
        '' + date.getFullYear() + (date.getMonth() + 1) + date.getDate();
      var storeAs = dir + '/' + time + '-' + date.getTime() + '.jpg';
      var reader = new FileReader();
      reader.readAsArrayBuffer(this.dataURLtoFile(base64));
      reader.onload = event => {
        var buffer = new OSS.Buffer(event.target.result);
        client.put(storeAs, buffer, {}).then(results => {
          let res = 'https://img.padj.group/' + results.name;
          resolve(res);
        });
      };
    });
  },
  dataURLtoFile(dataurl) {
    const arr = dataurl.split(',');
    const mime = arr[0].match(/:(.*?);/)[1];
    const bstr = atob(arr[1]);
    let n = bstr.length;
    const u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
  }
};
